package org.example.qqrobot.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.example.qqrobot.model.Result;
import org.example.qqrobot.model.dto.ProductNewDTO;
import org.example.qqrobot.model.dto.ProductQueryDTO;
import org.example.qqrobot.model.pojo.Product;
import org.example.qqrobot.model.vo.ProductIntoPageVO;
import org.example.qqrobot.model.vo.ProductMainVO;
import org.example.qqrobot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/getList")
    public Result getList(@RequestBody ProductQueryDTO productQueryDTO){
        log.info("获取查询产品的相关信息，{}",productQueryDTO);

        List<ProductMainVO> list = productService.getList(productQueryDTO);

        return Result.success(list);
    }

    @GetMapping("/intoPage")
    public Result intoPage(Long id){
        log.info("获取商品的详细的页面信息，{}",id);

        ProductIntoPageVO productIntoPageVO = productService.intoPage(id);
        return Result.success(productIntoPageVO);

    }

    @PostMapping("/newAProduct")
    public Result newAProduct(@RequestParam("title") String title,@RequestParam("price") Integer price
    ,@RequestParam("introduction") String introduction,@RequestParam("image") String image){
        log.info("新增商品，{},{},{},{}",title,price,introduction,image);

        productService.save(title,price,introduction,image);

        return Result.success(1);
    }

    @GetMapping("/collect")
    public Result collect(Long id){
        log.info("收藏商品，{}",id);

        productService.collect(id);
        return Result.success();
    }

    @PostMapping("/search")
    public Result search(@RequestParam("queryName") String queryName){
        log.info("搜索信息,{}",queryName);

       List<ProductMainVO> pList = productService.query(queryName);
       if(pList.size() == 0){
           return Result.success();
       }
       return Result.success(pList);

    }





}
